﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using HomeDMSDomain;
using System.Windows.Threading;
using System.Linq;

namespace bind_n_find
{
    /// <summary>
    /// Modes in which the 'new-edit' dialog can be opened
    /// </summary>
    public enum dlg_mode_e
    {
        new_t,
        view_t,
        edit_t,
    }


    /// <summary>
    /// Interaction logic for the 'new_edit' dialog
    /// </summary>
    public partial class new_edit_dlg : Window, System.IDisposable
    {
        #region private fields

        private BindnFindObjectsDataContext _currentContext;
        private dlg_mode_e _dlg_mode = dlg_mode_e.view_t;

        #endregion //private fields

        #region public properties
        public dlg_mode_e dlg_mode
        {
            set { _dlg_mode = value; }
            get { return _dlg_mode; }
        }

        #endregion //public properties

        #region events

        public static event EventHandler bi_list_updated;

        #endregion //events

        #region delegates

        private delegate void SimpleDelegate();

        #endregion //delegates

        #region construction and initialization



        public new_edit_dlg(dlg_mode_e dlg_mode)
        {
            InitializeComponent();
            _dlg_mode = dlg_mode;
            new_ctrl1.dlg_mode = dlg_mode;
            view_ctrl1.dlg_mode = dlg_mode;
        }

        #endregion //construction and initialization

        #region event handlers

        private void new_edit_dlg1_Loaded(object sender, RoutedEventArgs e)
        {
            _load_data();
        }


        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {

         //   tabControl1.Width = this.Width;
         //   tabControl1.Height = this.Height;

        }

        private void new_edit_dlg1_Closed(object sender, EventArgs e)
        {
        }
        #endregion //event handlers

        #region methods
        public void Dispose()
        {
            _free_memory();

        }

        /// <summary>
        /// Dispose some objects
        /// </summary>
        private void _free_memory()
        {
            List<image_text_item_template> bi_list = (List<image_text_item_template>)Application.Current.Properties["BitmapImagesList"];
            if (bi_list != null)
            {
                foreach (image_text_item_template image_item in bi_list)
                {
                    image_item.image.StreamSource.Dispose();
                    image_item.image.StreamSource.Close();

                }
            }
            bi_list.Clear();
            Application.Current.Properties["BitmapImagesList"] = null;
            tabControl1.Items.Clear();
        }




        private void _load_data()
        {
            if (_dlg_mode == dlg_mode_e.edit_t || _dlg_mode == dlg_mode_e.new_t)
            {
                tabControl1.SelectedIndex = 1;
            }
            else
            {
                tabControl1.SelectedIndex = 0;
            }
            if (_dlg_mode == dlg_mode_e.new_t)
                return;

            SimpleDelegate del1 = delegate()
            {
                //load document images to image list on the background
                List<image_text_item_template> bi_list = (List<image_text_item_template>)Application.Current.Properties["BitmapImagesList"];
                if (bi_list == null)
                    bi_list = new List<image_text_item_template>();
                if (bi_list != null)
                    bi_list.Clear();
                _currentContext = BindnFindObjectsDataContext.Instance;
                if (_currentContext == null)
                    return;
                Document doc = _currentContext.Documents.Single<Document>(d => d.document_id == Properties.Settings.Default.CurrentDocumentID);
                if (doc==null)
                    return;
                var q = from p in doc.Pages orderby p.Order select p;
                foreach (HomeDMSDomain.Page page in q)
                {
                    try
                    {
                        bi_list.Add(new image_text_item_template(imaging_c.image_to_bmp(page.Image), page.Name, page.page_id));
                    }
                    catch (Exception ex)
                    {
                        int i =0;
                    }

                }
                Application.Current.Properties["BitmapImagesList"] = bi_list;

                bi_list_updated(this, EventArgs.Empty);


            };


            this.Dispatcher.BeginInvoke(del1, DispatcherPriority.Background);
        }
        #endregion //methods

    }
}
